/**
 * <p>Title: ZMCMC0.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 余虎
 * @version 1.0
 * <pre>Histroy:
 *          2011-6-30 下午05:13:39  余虎  Create
 *</pre>
 */
package com.baosight.sczx.zm.cm.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.Department;
import com.baosight.sczx.common.zm.cInterface.cs.InitialData;
import com.baosight.sczx.zm.cm.domain.ZMCMTurnoverMaterialBase;
import com.baosight.sczx.zm.cm.domain.ZMCMTurnoverRecord;

/**材料回收管理--发料/回收
 * <p>Title: ZMCMC0.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 余虎
 * @version 1.0
 * <pre>Histroy:
 *          2011-6-30 下午05:13:39  余虎  Create
 *</pre>
 */
public class ServiceZMCMC0 extends ServiceEPBase {
	
	/**初始化数据
	 * <p>Function: initLoad</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCMC0</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-30下午05:15:54 余虎
	 * </p>
	 *
	*/
	@Override
	public EiInfo initLoad(EiInfo inInfo) {
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		EiInfo outInfo = this.query(inInfo);
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cs.units"));//构造计量单位下拉框
		outInfo.addBlock(Department.getInstance().getConstructionUnit("cmes.cm.org", "orgCode", "orgName", false));

		return outInfo;
	}
	/**查询
	 * <p>Function: query</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCMC0</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-30下午05:15:54 余虎
	 * </p>
	 *
	*/
	@Override
	public EiInfo query(EiInfo inInfo) {
		EiInfo outInfo = super.query(inInfo, "ZMCMTurnoverRecord.query",new ZMCMTurnoverRecord());
		outInfo.addBlock(Department.getInstance().getConstructionUnit("cmes.cm.org", "orgCode", "orgName", false));
		return outInfo;
	}

	/**
	 * <p>Function: export</p>
	 * <p>Description: 发料</p>
	 * <p>return value: ServiceZMCMC0</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-30下午05:15:54 余虎
	 * </p>
	 *
	*/
	public EiInfo export(EiInfo inInfo) 
	{
		inInfo.getBlock("result").setCell(0, "fdKeyId", TablePk.getInstance().getNextValue("T_CM_TURNOVERRECORD"));//主键
		EiInfo outInfo = super.insert(inInfo, "ZMCMTurnoverRecord.insert");
		if(outInfo.getStatus() == -1)
		{
			return outInfo;
		}
		updateTurnoverMaterialBase(inInfo, true, true);
		return outInfo;
	}
	
	/**
	 * 
	 * <p>Function: updateTurnoverMaterialBase</p>
	 * <p>Description: </p>
	 * <p>return value:void</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-12-2上午03:35:48 严洪浩
	 * </p>
	 *
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	private void updateTurnoverMaterialBase(EiInfo inInfo, boolean exportFlag, boolean sameMaterial)
	{
		Map inMap = new HashMap();
		if(exportFlag)
		{
			inMap.put("fdKeyId", inInfo.getBlock("result").getCellStr(0,"fdTrunoverMaterialId"));
			inMap.put("fdNum", String.valueOf(0-Double.parseDouble(inInfo.getBlock("result").getCellStr(0,"fdActualNumber"))));
			
			dao.update("ZMCMTurnoverMaterialBase.updateBaseNum", inMap);
		}
		else
		{
			if(!sameMaterial)
			{
				String fdTrunoverMaterialId = TablePk.getInstance().getNextValue("T_CM_TURNOVERMATERIALBASE");
				inMap.put("fdKeyId", fdTrunoverMaterialId);
				inMap.put("fdBusinessType", inInfo.getBlock("result").getCellStr(0,"fdBusinessType"));
				inMap.put("fdMaterialCode", inInfo.getBlock("result").getCellStr(0,"fdMaterialCode"));
				inMap.put("fdMaterialName", inInfo.getBlock("result").getCellStr(0,"fdMaterialName"));
				inMap.put("fdSpecModel", inInfo.getBlock("result").getCellStr(0,"fdSpecModel"));
				inMap.put("fdNumber", inInfo.getBlock("result").getCellStr(0,"fdNumber"));
				inMap.put("fdQuality", inInfo.getBlock("result").getCellStr(0,"fdQuality"));
				inMap.put("fdMetric", inInfo.getBlock("result").getCellStr(0,"fdMetric"));
				inMap.put("fdNum", inInfo.getBlock("result").getCellStr(0,"fdImportNumber"));
				inMap.put("fdRestorePlace", inInfo.getBlock("result").getCellStr(0,"fdRestorePlace"));
				inMap.put("fdRemark", inInfo.getBlock("result").getCellStr(0,"fdRemark"));
				
				dao.update("ZMCMTurnoverMaterialBase.insert", inMap);
				
				if(inInfo.getBlock("result").getCellStr(0,"fdTrunoverMaterialId") == null || "".equals(inInfo.getBlock("result").getCellStr(0,"fdTrunoverMaterialId")))
				{
					inMap = new HashMap();
					inMap.put("fdTrunoverMaterialId", fdTrunoverMaterialId);
					inMap.put("fdKeyId", inInfo.getBlock("result").getCellStr(0,"fdKeyId"));
					dao.update("ZMCMTurnoverRecord.updateTrunoverMaterialId", inMap);
				}
			}
			else
			{
				if(inInfo.getBlock("result").getCellStr(0,"fdTrunoverMaterialId") == null ||"".equals(inInfo.getBlock("result").getCellStr(0,"fdTrunoverMaterialId")))
				{
					inMap.put("fdMaterialCode", inInfo.getBlock("result").getCellStr(0,"fdMaterialCode"));
					inMap.put("fdMaterialName", inInfo.getBlock("result").getCellStr(0,"fdMaterialName"));
					inMap.put("fdSpecModel", inInfo.getBlock("result").getCellStr(0,"fdSpecModel"));
					inMap.put("fdNum", inInfo.getBlock("result").getCellStr(0,"fdImportNumber"));
				
					dao.update("ZMCMTurnoverMaterialBase.updateBaseNumWithNoId", inMap);
				}
				else
				{
					inMap.put("fdKeyId", inInfo.getBlock("result").getCellStr(0,"fdTrunoverMaterialId"));
					inMap.put("fdNum", inInfo.getBlock("result").getCellStr(0,"fdImportNumber"));
				
					dao.update("ZMCMTurnoverMaterialBase.updateBaseNum", inMap);
				}
			}
		}
	}
	
	/**
	 * 
	 * <p>Function: updateRecordStatus</p>
	 * <p>Description: </p>
	 * <p>return value:void</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-12-2上午03:36:07 严洪浩
	 * </p>
	 *
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	private void updateRecordStatus(String fdKeyId, String fdStatus)
	{
		Map inMap = new HashMap();
		inMap.put("fdKeyId", fdKeyId);
		inMap.put("fdStatus", fdStatus);
			
		dao.update("ZMCMTurnoverRecord.updateStatus", inMap);
	}

	/**
	 * <p>Function: importMaterial</p>
	 * <p>Description: 回收</p>
	 * <p>return value: ServiceZMCMC0</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-30下午05:15:54 余虎
	 * </p>
	 *
	*/
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public EiInfo importMaterial(EiInfo inInfo)
	{
		String fdImportNumber = inInfo.getBlock("result").getCellStr(0, "fdImportNumber");
		String fdBusinessType = inInfo.getBlock("result").getCellStr(0,"fdBusinessType");
		
		if("周转".equals(fdBusinessType) && Double.parseDouble("0.0") == Double.parseDouble(fdImportNumber))
		{
			inInfo.setStatus(-1);
			inInfo.setMsg("回收数量为0，请进行回收处理！");
			return inInfo;
		}
		
		String detailkeyId = inInfo.getBlock("result").getCellStr(0, "fdKeyId");
		inInfo.getBlock("result").setCell(0, "fdSourceKeyId", detailkeyId);
		inInfo.getBlock("result").setCell(0, "fdKeyId", TablePk.getInstance().getNextValue("T_CM_TURNOVERRECORD"));//主键
		EiInfo outInfo = super.insert(inInfo, "ZMCMTurnoverRecord.insert");
		if(outInfo.getStatus() == -1)
		{
			return outInfo;
		}
		
		if("周转".equals(fdBusinessType))
		{
			boolean sameFlag = false;
			Map inMap = new HashMap();
			inMap.put("fdKeyId", inInfo.getBlock("result").getCellStr(0,"fdTrunoverMaterialId"));
			inMap.put("fdMaterialCode", inInfo.getBlock("result").getCellStr(0,"fdMaterialCode"));
			inMap.put("fdMaterialName", inInfo.getBlock("result").getCellStr(0,"fdMaterialName"));
			inMap.put("fdSpecModel", inInfo.getBlock("result").getCellStr(0,"fdSpecModel"));
			inMap.put("fdNumber", inInfo.getBlock("result").getCellStr(0,"fdNumber"));
			List _list = dao.query("ZMCMTurnoverMaterialBase.querySameMaterial", inMap);
			if(_list.size() > 0)
			{
				sameFlag = true;
			}
			
			updateTurnoverMaterialBase(inInfo, false, sameFlag);
			updateRecordStatus(detailkeyId, "已周转");
		}
		else
		{
			updateRecordStatus(detailkeyId, "已回收");
		}
		
		return outInfo;
	}
	/**
	 * <p>Function: delete</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCMC0</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-30下午05:15:54 余虎
	 * </p>
	 *
	*/
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public EiInfo delete(EiInfo inInfo)
	{
		String fdKeyId = inInfo.getBlock(EiConstant.resultBlock).getCellStr(0, "fdKeyId");
		String fdSourceKeyId = inInfo.getBlock(EiConstant.resultBlock).getCellStr(0, "fdSourceKeyId");
		String fdBusinessType = inInfo.getBlock(EiConstant.resultBlock).getCellStr(0, "fdBusinessType");
		String fdTrunoverStatus = inInfo.getBlock(EiConstant.resultBlock).getCellStr(0, "fdTrunoverStatus");
		String fdTrunoverMaterialId = inInfo.getBlock(EiConstant.resultBlock).getCellStr(0, "fdTrunoverMaterialId");
		String fdActualNumber = inInfo.getBlock(EiConstant.resultBlock).getCellStr(0, "fdActualNumber");
		String fdImportNumber = inInfo.getBlock(EiConstant.resultBlock).getCellStr(0, "fdImportNumber");
		
		String str_fdChangedNum = inInfo.getBlock(EiConstant.resultBlock).getCellStr(0, "fdChangedNum");
		double db_fdChangedNum = Double.parseDouble(str_fdChangedNum == null? "0": str_fdChangedNum);
		if(db_fdChangedNum > Double.parseDouble("0.0"))
		{
			EiInfo outInfo = new EiInfo();
			outInfo.setStatus(-1);
			outInfo.setMsg("选择的记录已经完成交旧领新业务，不能进行红冲！");
			return outInfo;
		}
		
		if("发料".equals(fdTrunoverStatus))
		{
			if(fdTrunoverMaterialId == null || "".equals(fdTrunoverMaterialId))
			{
				EiInfo outInfo = new EiInfo();
				outInfo.setStatus(-1);
				outInfo.setMsg("选择的记录为出库时所选业务类型数据，不能进行红冲！");
				return outInfo;
			}
			else
			{
				inInfo.getBlock("result").setCell(0, "fdKeyId", TablePk.getInstance().getNextValue("T_CM_TURNOVERRECORD"));//主键
				inInfo.getBlock("result").setCell(0, "fdTrunoverStatus", "红冲");
				inInfo.getBlock("result").setCell(0, "fdActualNumber", 0-Double.parseDouble(fdActualNumber));
				EiInfo outInfo = super.insert(inInfo, "ZMCMTurnoverRecord.insert");
				
				if(outInfo.getStatus() == -1)
				{
					return outInfo;
				}
				updateTurnoverMaterialBase(inInfo, true, true);
				updateRecordStatus(fdKeyId, "已红冲");
				return outInfo;
			}
		}
		else
		{
			if("回收".equals(fdBusinessType))
			{
				inInfo.getBlock("result").setCell(0, "fdKeyId", TablePk.getInstance().getNextValue("T_CM_TURNOVERRECORD"));//主键
				inInfo.getBlock("result").setCell(0, "fdTrunoverStatus", "红冲");
				inInfo.getBlock("result").setCell(0, "fdActualNumber", 0-Double.parseDouble(fdActualNumber));
				inInfo.getBlock("result").setCell(0, "fdImportNumber", 0-Double.parseDouble(fdImportNumber));
				EiInfo outInfo = super.insert(inInfo, "ZMCMTurnoverRecord.insert");
				updateRecordStatus(fdKeyId, "已红冲");
				
				if(outInfo.getStatus() == -1)
				{
					return outInfo;
				}
			}
			else
			{
				inInfo.getBlock("result").setCell(0, "fdKeyId", TablePk.getInstance().getNextValue("T_CM_TURNOVERRECORD"));//主键
				inInfo.getBlock("result").setCell(0, "fdTrunoverStatus", "红冲");
				inInfo.getBlock("result").setCell(0, "fdActualNumber", 0-Double.parseDouble(fdActualNumber));
				inInfo.getBlock("result").setCell(0, "fdImportNumber", 0-Double.parseDouble(fdImportNumber));
				EiInfo outInfo = super.insert(inInfo, "ZMCMTurnoverRecord.insert");
				
				updateRecordStatus(fdKeyId, "已红冲");
				
				if(outInfo.getStatus() == -1)
				{
					return outInfo;
				}
				
				Map inMap = new HashMap();
				inMap.put("fdKeyId", fdTrunoverMaterialId);
				List _list = dao.query("ZMCMTurnoverMaterialBase.query", inMap);
				if(_list.size() > 0)
				{
					ZMCMTurnoverMaterialBase base = (ZMCMTurnoverMaterialBase)_list.get(0);
					if(base.getFdNum() == Double.parseDouble("0.0"))
					{
						outInfo.setStatus(-1);
						outInfo.setMsg("选择的记录对应回收材料库存为0，不能进行红冲！");
						return outInfo;
					}
					else if(base.getFdNum() < Double.parseDouble(fdImportNumber))
					{
						outInfo.setStatus(-1);
						outInfo.setMsg("选择的记录对应回收材料的库存量小于红冲记录的回收数量，不能进行红冲！");
						return outInfo;
					}
					else
					{
						inInfo.getBlock("result").setCell(0, "fdActualNumber", Double.parseDouble(fdImportNumber));
						updateTurnoverMaterialBase(inInfo, true, true);
					}
				}
				else
				{
					outInfo.setStatus(-1);
					outInfo.setMsg("选择的记录对应回收材料库存为0，不能进行红冲！");
					return outInfo;
				}
			}
			
			updateRecordStatus(fdSourceKeyId, "");
			return this.query(inInfo);
		}
	}
	
	/**
	 * 材料回收customview公共选择查询方法
	 * <p>Function: queryforCustomview</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-7-1上午10:08:11 余虎
	 * </p>
	 *
	 */
	public EiInfo queryForMaterialsCustomView(EiInfo inInfo)
	{
		EiInfo outInfo = super.query(inInfo, "ZMCMTurnoverMaterialBase.query", new ZMCMTurnoverMaterialBase());
		return outInfo;
	}
	
}
